package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.ColumnSetAddDTO;
import com.xbongbong.paas.pojo.dto.UpdateMobileFocusModeDTO;
import com.xbongbong.paas.pojo.dto.UpdateMobileModeDTO;
import com.xbongbong.paas.pojo.dto.UserConfigAddDTO;
import com.xbongbong.paas.pojo.vo.ColumnSetAddVO;
import com.xbongbong.paas.pojo.vo.MobileFocusModeGetVO;
import com.xbongbong.paas.pojo.vo.MobileMenuModeSetGetVO;
import com.xbongbong.paas.pojo.vo.MobileModeGetVO;
import com.xbongbong.paas.pojo.vo.UpdateMobileFocusModeVO;
import com.xbongbong.paas.pojo.vo.UpdateMobileModeVO;
import com.xbongbong.paas.pojo.vo.UserConfigAddVO;
import com.xbongbong.paas.service.UserConfigService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author long.rao
 * @version v1.0
 * @date 2019/9/2 10:43
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/config")
public class MobileConfigController {

    @Resource
    private UserConfigService userConfigService;

    /**
     * 列表设置
     * @param columnSetAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/column/set/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String columnSetAdd(@RequestBody @Valid ColumnSetAddDTO columnSetAddDTO, BindingResult br) {
        XbbResponse<ColumnSetAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ColumnSetAddVO columnSetAddVO = userConfigService.columnSetAdd(columnSetAddDTO);
                response = new XbbResponse<>(columnSetAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    @RequestMapping(value = "/update/mobileMode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateMobileMode(@RequestBody @Valid UpdateMobileModeDTO updateMobileModeDTO, BindingResult br) {
        XbbResponse<UpdateMobileModeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                UpdateMobileModeVO updateMobileModeVO = userConfigService.updateMobileMode(updateMobileModeDTO);
                response = new XbbResponse<>(updateMobileModeVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/get/mobileMode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getMobileMode(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<MobileModeGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MobileModeGetVO mobileModeGetVO = userConfigService.getMobileMode(baseDTO);
                response = new XbbResponse<>(mobileModeGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/update/mobileFocusMode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateMobileFocusMode(@RequestBody @Valid UpdateMobileFocusModeDTO updateMobileFocusModeDTO, BindingResult br) {
        XbbResponse<UpdateMobileFocusModeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                UpdateMobileFocusModeVO updateMobileFocusModeVO = userConfigService.updateMobileFocusMode(updateMobileFocusModeDTO);
                response = new XbbResponse<>(updateMobileFocusModeVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/get/mobileFocusMode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getMobileFocusMode(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<MobileFocusModeGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MobileFocusModeGetVO mobileFocusModeGetVO = userConfigService.getMobileFocusMode(baseDTO);
                response = new XbbResponse<>(mobileFocusModeGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/get/getMobileMenuModeSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getMobileMenuModeSet(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<MobileMenuModeSetGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MobileMenuModeSetGetVO mobileMenuModeSetGetVO = userConfigService.getMobileMenuModeSet(baseDTO);
                response = new XbbResponse<>(mobileMenuModeSetGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/product/openOnlyShowSpecifitions", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String openOnlyShowSpecifitions(@RequestBody @Valid UserConfigAddDTO userConfigAddDTO, BindingResult br) {
        XbbResponse<UserConfigAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                UserConfigAddVO userConfigAddVO = userConfigService.openOnlyShowSpecifitions(userConfigAddDTO);
                response = new XbbResponse<>(userConfigAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
